package com.example.ktapplication.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.widget.FrameLayout
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.example.ktapplication.R
import com.example.ktapplication.base.BaseActivity
import com.example.ktapplication.databinding.ActivityWebviewBinding
import com.example.ktapplication.utils.LogHelper
import com.example.ktapplication.utils.toJson
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebViewClient

class WebViewActivity : BaseActivity<ActivityWebviewBinding>() {

    private val data by lazy {
        intent?.getStringExtra(EXTRA_DATA) ?: ""
    }

    private val title by lazy {
        intent?.getStringExtra(EXTRA_TITLE) ?: ""
    }

    private val url by lazy {
        intent.getStringExtra(EXTRA_URL) ?: ""
    }

    private val webView by lazy {
        AgentWeb.with(this)
            .setAgentWebParent(
                binding.webContainer,
                FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT
                )
            )
            .useDefaultIndicator()
            .setWebViewClient(WebViewClient())
            .addJavascriptInterface("webkit", JsObject())
            .createAgentWeb()
            .ready()
    }

    companion object {
        private const val EXTRA_DATA = "data"
        private const val EXTRA_TITLE = "title"
        private const val EXTRA_URL = "url"

        /**
         * 打开H5链接
         * @param url H5链接
         * @param title 标题，不传时，则隐藏标题栏
         * @param obj 传对象，预留参数，待后续完善
         */
        fun start(context: Context, url: String, title: String = "", obj: Any? = null) {
            LogHelper.e("---ext webView start >>>> title:$title obj:${obj?.toJson()} url:$url")
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra(EXTRA_URL, url)
            intent.putExtra(EXTRA_TITLE, title)
            obj?.let {
                intent.putExtra(EXTRA_DATA, it.toJson())
            }
            context.startActivity(intent)
        }
    }

    override fun getViewBind(): ActivityWebviewBinding {
        return ActivityWebviewBinding.inflate(layoutInflater)
    }

    override fun onDate(savedInstanceState: Bundle?) {
    }

    @SuppressLint("ResourceType")
    override fun onViewBing() {
        initTitleBar()
        LogHelper.e("---ext webView url >>>> $url")
        webView.go(url)
    }

    private fun initTitleBar() {
        val showTitleBar = title.isNotEmpty()
        binding.titleBar.isVisible = showTitleBar
        if (showTitleBar) {
            binding.titleBar.tvTitle?.text = title
            binding.titleBar.imgMenu?.apply {
                Glide.with(context)
                    .load(R.mipmap.left)
                    .into(this)
                setOnClickListener {
                    finish()
                }
            }
            binding.titleBar.imgFind?.isGone = true
        }
    }

    override fun onPhoneBackPressed() {
        if (!webView.get().back()) {
            finish()
        }
    }

    /**
     * js回调，预留
     * 注意，必须有声明，才能注册该对象，否则，会闪退
     */
    inner class JsObject {
        //示例js方法，供H5调用
        @JavascriptInterface
        fun test() {

        }
    }

}